package com.qys.livesMall.shop.event.handle.service;

import com.qys.livesMall.shop.event.handle.BaseHandler;
import com.qys.livesMall.shop.event.shop.pojo.ShopIncomePojo;
import com.qys.livesMall.shop.modal.entity.H2ShopStatisticsInfo;
import com.qys.livesMall.shop.modal.entity.ShopEventInfo;
import com.qys.livesMall.shop.modal.entity.ShopStatisticsEveryDay;
import com.qys.livesMall.shop.service.H2ShopStatisticsInfoService;
import com.qys.livesMall.shop.service.ShopStatisticsEverydayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * @menu ShopBaseServer
 * @date 2023/11/03 10:09
 */
@Service
public class ShopIncomeServer implements BaseHandler<ShopIncomePojo> {

    @Autowired
    private H2ShopStatisticsInfoService shopStatisticsInfoService;
    @Autowired
    private ShopStatisticsEverydayService shopStatisticsEverydayService;

    @Override
    public void correct(Long shopId) {


    }

    @Override
    public void handle(ShopEventInfo<ShopIncomePojo> info) {
        ShopStatisticsEveryDay shopStatisticsEveryday=shopStatisticsEverydayService.getField(info.getShopId(), LocalDate.now(),ShopStatisticsEveryDay::getId, ShopStatisticsEveryDay::getIncome);
        if (shopStatisticsEveryday==null){
            shopStatisticsEveryday=new ShopStatisticsEveryDay();
            shopStatisticsEveryday.setShopId(info.getShopId());
            shopStatisticsEveryday.setDataDate(LocalDate.now());
            shopStatisticsEveryday.setIncome(BigDecimal.ZERO);
        }
        shopStatisticsEveryday.setIncome(shopStatisticsEveryday.getIncome().add(info.getParamObj().getAmountIncurred()));
        shopStatisticsEverydayService.saveOrUpdate(shopStatisticsEveryday);
        H2ShopStatisticsInfo shopStatisticsInfo =shopStatisticsInfoService.getField(info.getShopId(),H2ShopStatisticsInfo::getId, H2ShopStatisticsInfo::getShopId,H2ShopStatisticsInfo::getCumulative);
        if (shopStatisticsInfo.getCumulative()==null){
            shopStatisticsInfo.setCumulative(BigDecimal.ZERO);
        }
        shopStatisticsInfo.setCumulative(shopStatisticsInfo.getCumulative().add(info.getParamObj().getAmountIncurred()));
        shopStatisticsInfoService.updateById(shopStatisticsInfo);
    }
}

